<?php

namespace app\shop\controller;

use think\facade\Db;

class Task
{
    public function jinjianWxpayState()
    {
        $list = Db::name('jinjian_wxpay')
            ->where([
                ['applyment_state', 'in', ['APPLYMENT_STATE_AUDITING', 'APPLYMENT_STATE_TO_BE_CONFIRMED', 'APPLYMENT_STATE_TO_BE_SIGNED', 'APPLYMENT_STATE_SIGNING']]
            ])
            ->field('shop_id')
            ->select();
        foreach($list as $v) {
            $this->flushApplyStatus($v['shop_id']);
        }
    }
    /**
     * 查询申请单状态
     */
    private function flushApplyStatus($shop_id)
    {
        $info = Db::name('jinjian_wxpay')
            ->where('shop_id', $shop_id)
            ->find();
        if (!$info || empty($info['applyment_id'])) {
            return errorJson('刷新失败，没有找到进件记录');
        }
        $WechatClass = $this->newWechat($info['server_id']);
        $result = $WechatClass->enquiryOfApplyStatus(['applyment_id' => $info['applyment_id']]);
        Db::name('jinjian_wxpay')
            ->where('applyment_id', $info['applyment_id'])
            ->update([
                'applyment_state' => $result['applyment_state'],
                'applyment_state_msg' => $result['applyment_state_msg'],
                'sub_mchid' => isset($result['sub_mchid']) ? $result['sub_mchid'] : '',
                'audit_detail' => isset($result['audit_detail']) ? json_encode($result['audit_detail']) : '',
                'sign_url' => isset($result['audit_detail']) ? $result['sign_url'] : '',
                'update_time' => time()
            ]);
        if (empty($info['sub_mchid']) && !empty($result['sub_mchid'])) {
            // 获取费率
            if ($info['activities_rate']) {
                $rate = $info['activities_rate'] . '%';
            } else {
                $rate = Db::name('jinjian_wxpay_trade')
                    ->where('settlement_id', $info['settlement_id'])
                    ->value('rate');
            }

            Db::name('shop')
                ->where('id', $info['shop_id'])
                ->update([
                    'wxpay_config' => json_encode([
                        'wxpay_status'=>1,
                        'wxpay_type' => 'server',
                        'wxpay_server_id' => $info['server_id'],
                        'wxpay_mch_id' => $result['sub_mchid'],
                        'wxpay_rate' => $rate
                    ])
                ]);
        }
        echo $info['applyment_id'] . '最新状态：' . $result['applyment_state'] . "\n";
    }

    /**
     * 初始化微信进件类
     */
    private function newWechat($server_id)
    {
        $config = Db::name('server')
            ->where('id', $server_id)
            ->value('config');
        $config = json_decode($config, true);

        $option = [
            'appid' => $config['appid'],
            'secret' => $config['appsecret'],
            'mch_id' => $config['mch_id'],
            'aes_key' => $config['key'],
            'diy_key' => $config['key'],  //自定义key
            'apiclient_cert' => '.' . mediaUrl($config['apiclient_cert']),
            'apiclient_key' => '.' . mediaUrl($config['apiclient_key'])
        ];
        return new \Wxpay\jinjian\Wechat($option);
    }
}
